using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System.IO;

namespace SgsUI
{
    [RequireComponent(typeof(RectTransform))]
    [RequireComponent(typeof(SpriteRes))]
    public class ViewSprite : ViewNode
    {
        [SerializeField] private string res;

        [SerializeField] private bool scale9;

        public string real_sprite;

        SpriteRes m_spriteRes;
        
        protected ViewSprite()
        {
            type = "sprite";
        }

        public override void UpdateParam()
        {
            base.UpdateParam();
            UpdateImage();
        }

        public void UpdateImage()
        {
            if(m_spriteRes == null)
                m_spriteRes = GetComponent<SpriteRes>();

            if(m_spriteRes == null)
                return;

            res = m_spriteRes.GetResName();

            scale9 = m_spriteRes.type == Image.Type.Sliced;
        }

        protected override string GetDefaultName()
        {
            return "sprite";
        }

        public override bool HasAtriSize()
        {
            if(m_spriteRes == null)
                m_spriteRes = GetComponent<SpriteRes>();

            return m_spriteRes.type == Image.Type.Sliced;
        }

        protected override void WriteOtherParam(NodeData nodeData)
        {
            if(real_sprite != String.Empty)
            {
                ParamData param_res = new ParamData();
                param_res.SetValue("res",real_sprite);
                nodeData.AddParam(param_res);
            }
            else if(res != String.Empty)
            // if(res != String.Empty)
            {
                ParamData param_res = new ParamData();
                param_res.SetValue("res",res);
                nodeData.AddParam(param_res);
            }

            if(m_spriteRes.type == Image.Type.Sliced)
            {
                ParamData param_scale9 = new ParamData();
                param_scale9.SetValue("scale9",true);
                nodeData.AddParam(param_scale9);
            }
        }
    }
}